package com.zdy.boot.upload.core.job.runner;

import com.zdy.boot.upload.core.job.config.JobConfig;
import com.zdy.boot.upload.core.job.config.JobHandleConfig;
import com.zdy.boot.upload.core.job.scheduler.ScheduleManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * @author sdy
 * @description
 * @date 2025/2/9
 */
@Slf4j
@Component
public class JobRunner implements CommandLineRunner {

    @Resource
    private JobConfig jobConfig;
    @Resource
    private ScheduleManager scheduleManager;

    @Override
    public void run(String... args) throws Exception {
        if (CollectionUtils.isEmpty(jobConfig.getHandles())) {
            log.info("没有需要加载的任务");
        }
        log.info("加载定时任务开始");
        for (JobHandleConfig job : jobConfig.getHandles()) {
            scheduleManager.addJob(job.getJobId(), job.getHandleName(), job.getHandleParams(), job.getCronExpression(),
                    job.getRetryTimes(), job.getRetryInterval());
        }
        log.info("加载定时任务完成");
    }

}
